package com.android.smsmessing;

import android.app.Activity;
import android.app.ActivityGroup;
import android.app.LocalActivityManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.gsm.SmsManager;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.android.smsmessing.services.SMSService;

public class SMSSendActivity extends ActivityGroup{
	//内容布局
	private LinearLayout contentViewLayout;
	//
	private View contentView;
	private LinearLayout.LayoutParams contentViewLayoutParams;
	private LocalActivityManager activityManager;
	private Context context;
	//发送栏布局
	private LinearLayout sendMessageLayout;
	//发送文本
	private EditText txtSendMessage;
	//发送按钮
	private Button btnSend;
	//当前电话
	private String number;
	//当前Intenet
	private Intent currentIntent;
	
	String SENT = "SMS_SENT";
	String DELIVERED = "SMS_DELIVERED";
	PendingIntent sentPI;
	PendingIntent deliveredPI;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		 context = this;
		 

			
			
		 activityManager = getLocalActivityManager();
		 setContentView(R.layout.customize_activity);
		 //内容布局
		 contentViewLayout = (LinearLayout)findViewById(R.id.SendMessageContentViewLayout);
		 sendMessageLayout = (LinearLayout) findViewById(R.id.sendMessageLayout);
		 txtSendMessage = (EditText) findViewById(R.id.txtSendMessage);
		 btnSend = (Button) findViewById(R.id.btnSend);
		 
		 
		 
		 contentViewLayoutParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
		 Bundle bundle = this.getIntent().getExtras();
		 number = bundle.getString("number");
		 
		 btnSend.setOnClickListener(btnSendSMSOnClick);

		 //启动短信列表
		Intent intent = new Intent();
		intent.setClass(this, SMSActivity.class);
		intent.putExtras(bundle);
		this.startGroupActivity("0", intent);
		
		sentPI = PendingIntent.getBroadcast(this, 0, new Intent(
				SENT), 0);

		deliveredPI = PendingIntent.getBroadcast(this, 0,
				new Intent(DELIVERED), 0);

		
	}
	
	BroadcastReceiver sentBroadcastReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			switch (getResultCode()) {
			case Activity.RESULT_OK:
				Toast.makeText(getBaseContext(), "SMS发送",
						Toast.LENGTH_SHORT).show();
				break;
			case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
				Toast.makeText(getBaseContext(), "初始化失败",
						Toast.LENGTH_SHORT).show();
				break;
			case SmsManager.RESULT_ERROR_NO_SERVICE:
				Toast.makeText(getBaseContext(), "没有开启服务",
						Toast.LENGTH_SHORT).show();
				break;
			case SmsManager.RESULT_ERROR_NULL_PDU:
				Toast.makeText(getBaseContext(), "没有PDU",
						Toast.LENGTH_SHORT).show();
				break;
			case SmsManager.RESULT_ERROR_RADIO_OFF:
				Toast.makeText(getBaseContext(), "没有开启广播",
						Toast.LENGTH_SHORT).show();
				break;
			}
		}

	};
	
	BroadcastReceiver deliveredBroadcastReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			switch (getResultCode()) {
			case Activity.RESULT_OK:
				Toast.makeText(getBaseContext(), "SMS发送成功",
						Toast.LENGTH_SHORT).show();
				break;
			case Activity.RESULT_CANCELED:
				Toast.makeText(getBaseContext(), "SMS未发送",
						Toast.LENGTH_SHORT).show();
				break;
			}
		}

	};
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		registerReceiver(sentBroadcastReceiver,new IntentFilter(SENT));
		registerReceiver(deliveredBroadcastReceiver, new IntentFilter(DELIVERED));
	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		unregisterReceiver(sentBroadcastReceiver);
		unregisterReceiver(deliveredBroadcastReceiver);
	}
	
	View.OnClickListener btnSendSMSOnClick = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			String message = txtSendMessage.getText().toString();
			if (message.length() > 0) {
				sendSMS(number, message);
			} else {
				Toast.makeText(getBaseContext(), "请输入手机号和消息",
						Toast.LENGTH_SHORT).show();
			}
		}

	};
	
	private void sendSMS(String phoneNumber, String message) {
		InputMethodManager imm = (InputMethodManager)context.getSystemService(INPUT_METHOD_SERVICE);
		if(imm != null){
			imm.hideSoftInputFromWindow(txtSendMessage.getWindowToken(), 0);
		}
		//发送短信
		SMSService.getInstance().sendSMS(context,phoneNumber, message, sentPI, deliveredPI);
		
		//刷新当前页面数据
		SMSActivity activity = (SMSActivity) contentView.getContext();
		activity.initData(activity, activity.smsAdapter, phoneNumber);
		//移动到最后一行
		activity.toLastItem(activity.getListView(), activity.smsAdapter.getSmses().size());
	}
	
    public void startGroupActivity(String id, Intent intent)
    {
    	contentViewLayout.removeAllViews();
    	currentIntent = intent;
    	View view = activityManager.startActivity(id, intent).getDecorView();
    	contentView = view;
        contentViewLayout.addView(view, contentViewLayoutParams);
    }

}
